package com.fankui.serviceedu.controller;


import com.fankui.commonutils.R;
import com.fankui.serviceedu.entity.EduChapter;
import com.fankui.serviceedu.entity.vo.ChapterVo;
import com.fankui.serviceedu.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author 范奎
 * @since 2023-11-25
 */
@Api(description = "课程章节管理")
@RestController
@RequestMapping("/eduservice/chapter")
public class EduChapterController {
    @Autowired
    private EduChapterService chapterService;

    @ApiOperation(value = "根据课程id获取课程大纲")
    @GetMapping("getChapterList/{courseId}")
    public R getChapterList(@ApiParam(name = "courseId", value = "课程id", required = true) @PathVariable String courseId) {
        List<ChapterVo> chapterList = chapterService.getChapterListByCourseId(courseId);
        return R.ok().data("chapterList", chapterList);
    }

    @ApiOperation(value = "添加课程章节")
    @PostMapping("addChapter")
    public R addChapter(@ApiParam(name = "chapter", value = "添加章节对象", required = true) @RequestBody EduChapter chapter) {
        boolean flag = chapterService.save(chapter);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "根据章节id查询章节信息")
    @GetMapping("getChapterById/{chapterId}")
    public R getChapterById(@ApiParam(name = "chapterId", value = "章节id", required = true) @PathVariable String chapterId) {
        EduChapter chapter = chapterService.getById(chapterId);
        return R.ok().data("chapter", chapter);
    }

    @ApiOperation(value = "修改课程章节")
    @PostMapping("updateChapter")
    public R updateChpater(@ApiParam(name = "chapter", value = "修改章节对象", required = true) @RequestBody EduChapter chapter) {
        boolean flag = chapterService.updateById(chapter);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "根据章节id删除章节信息")
    @DeleteMapping("remove/{chapterId}")
    public R removeChapter(@ApiParam(name = "chapterId", value = "章节id", required = true) @PathVariable String chapterId) {
        boolean flag = chapterService.removeChapterById(chapterId);
        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

}

